﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Manager {
    /// <summary>
    /// Sections
    /// </summary>
    public partial class HelpController : BaseController {

        public ActionResult SectionList([DefaultValue(1)]int number) {
            int pageCount, pageCount1;
            string typesIdRequest = Request["TypesId"];
            int typesId = string.IsNullOrEmpty(typesIdRequest)
                ? 0
                : int.Parse(typesIdRequest);
            List<HelpTypeEntity> typeList = _help.GetHelpTypeList(int.MaxValue, 1, out pageCount1);
            if (typeList != null) {
                typeList.Insert(0, new HelpTypeEntity() { Name = "所有分类" });
            }
            return View(new HelpSectionViewModel() {
                HelpSectionList = _help.GetHelpSectionList(typesId, this.PageSize, number, out pageCount),
                HelpTypeList = typeList,
                TypesId = typesId,
                PageNumber = number,
                Pager = this.CreatePager(Url.Action("SectionList", new { number = "{0}", TypesId = typesId }), pageCount, number)
            });
        }

        public ActionResult SectionEdit([DefaultValue(1)]int number, [DefaultValue(0)]int id) {
            List<HelpSectionEntity> listHelpSection = _help.GetHelpSectionList(string.Empty);
            int pageCount;
            return View(new HelpSectionViewModel() {
                HelpSection = id == 0 ? new HelpSectionEntity() {
                    Sequence = listHelpSection != null ? listHelpSection.Count + 1 : 1,
                } : _help.GetHelpSectionById(id),
                PageNumber = number,
                HelpTypeList = _help.GetHelpTypeList(int.MaxValue, 1, out pageCount)
            });
        }

        public JsonResult SectionShowById([DefaultValue(0)]int id) {
            return this.Json(_help.GetHelpSectionById(id), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SectionEdit([DefaultValue(1)]int number, HelpSectionViewModel hsv) {
            HelpSectionEntity hse = hsv.HelpSection;
            if (hse.SectionsId == 0) {
                _help.HelpSectionAppend(hse);
            }
            else {
                _help.HelpSectionUpdate(hse);
            }
            return this.RedirectToAction("SectionEdit", new { number = number, id = hse.SectionsId });
        }

        public ActionResult SectionDelete(string ids) {
            List<HelpSectionEntity> listHelpSection = _help.GetHelpSectionList(ids);
            _help.HelpSectionDelete(ids);

            return RedirectToAction("SectionList");
        }

        [HttpPost]
        public ActionResult SectionEditBatchSeq([DefaultValue(1)]int number, FormCollection fc) {
            _help.HelpSectionBatchUpdateSequence(fc["cbKey"], fc["tbSequence"]);

            return this.RedirectToAction("SectionList", new { number = number });
        }

    }
}